package com.zhtd.knocker.http;

import com.zhtd.knocker.util.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Http Request
 */
public class HttpRequest {

    private String mUrl;
    private int mConnectTimeout = -1;
    private int mReadTimeout = -1;

    private final Map<String, String> headerParamter = new HashMap<String, String>();
    private String body;

    public HttpRequest(String url) {
        mUrl = url;
    }

    public HttpRequest(String mUrl, int mConnectTimeout, int mReadTimeout,String body) {
        this.mUrl = mUrl;
        this.mConnectTimeout = mConnectTimeout;
        this.mReadTimeout = mReadTimeout;
        this.body = body;
    }

    public final void setConnectTimeout(int timeout) {
        mConnectTimeout = timeout;
    }

    public final void setReadTimeout(int timeout) {
        mReadTimeout = timeout;
    }

    public final int getConnectTimeout() {
        return mConnectTimeout;
    }

    public final int getReadTimeout() {
        return mReadTimeout;
    }

    public final void setUrl(String url) {
        mUrl = url;
    }

    public final String getUrl() {
        return mUrl;
    }

    public void addHeaderParameter(String key, String value) {
        if (!StringUtils.isEmpty(key) && value != null) {
            headerParamter.put(key, value);
        }
    }

    public Map<String, String> getHeaderParameter() {
        return headerParamter;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    @Override
    public String toString() {

        StringBuilder str = new StringBuilder();

        str.append("url=");
        str.append(mUrl);
        str.append("\n");

        // header
        Set<String> keys = headerParamter.keySet();
        for (String key : keys) {
            str.append(key);
            str.append("=");
            str.append(headerParamter.get(key));
            str.append("\n");
        }

        // paramter
        if(body != null){
            str.append("body=");
            str.append(body);
            str.append("\n");
        }

        return str.toString();
    }
}
